var isSymmetric = function (root) {
    if (root === null) return true;
    let node = root;
    let queue = [node];
    while (queue.length > 0) {
        if (!help(queue)) {
            return false;
        }
        let len = queue.length;
        for (let i = 0; i < len; i++) {
            node = queue.shift();
            if (node !== null) {
                queue.push(node.left);
                queue.push(node.right);
            }
        }
    }
    return true
};

function help(arr) {
    let left = 0, right = arr.length - 1;
    while (left < right) {
        if (arr[left] === null || arr[right] === null) {
            if (arr[left] !== arr[right]) {
                return false;
            }
        } else {
            if (arr[left].val !== arr[right].val) {
                return false;
            }
        }
        left++;
        right--;
    }
    return true;
}



// levelOrder

var levelOrder = function (root) {
    if (root == null) return [];
    const ans = [];
    let level = 0;
    const queue = [root];
    while (queue.length) {
        ans.push([]);
        const len = queue.length;
        // 通过遍历，提前执行完一层的所有元素，层级level就可以+1
        for (let i = 0; i < len; i++) {
            const node = queue.shift();
            ans[level].push(node.val);
            node.left && queue.push(node.left);
            node.right && queue.push(node.right);
        }
        level++;
    }
    return ans;
};
